<?php

class CartWS_Model_FormulaEngine_Db_FormulaRow extends Ext_Db_Table_Row
{
    public function getProperties() {
        return $this->findCartWS_Model_FormulaEngine_Db_FormulaProperty();
    }
}

class CartWS_Model_FormulaEngine_Db_Formula  extends Ext_Db_Table {

    const TYPE_PROMOTION = CartWS_Model_SalesOrderLine::LINE_TYPE_PROMOTION;
    const TYPE_PAYMENT_METHOD_CHARGE = CartWS_Model_SalesOrderLine::LINE_TYPE_PAYMENT_METHOD_CHARGE;
    const TYPE_SHIPPING_COST = CartWS_Model_SalesOrderLine::LINE_TYPE_SHIPPING_COST;

    public static $types = array(
    self::TYPE_SHIPPING_COST,
    self::TYPE_PROMOTION,
    self::TYPE_PAYMENT_METHOD_CHARGE
    );

    protected $_rowClass = "CartWS_Model_FormulaEngine_Db_FormulaRow";
    protected $_name = 'sc_formula';
    protected $_primary = 'formula_id';
    protected $_sequence = 'sc_formula_id_seq';

    private $_activeFormulas = null;
    public function findActiveFormulas() {
        if (  $this->_activeFormulas == null ) {
            $sql = $this
                ->select()
                ->where( "start_date is not null" )
                ->where( "start_date < ? ", Ext_Db_Date::now() )
                ->where( "( end_date is null or end_date > ?)" , Ext_Db_Date::now()  )
                ->order( "sequence asc");
            $this->_activeFormulas = $this->fetchAll( $sql );
        }
        return $this->_activeFormulas;
    }

}
